#ifndef _KEY_H_
#define _KEY_H_

#include "Global.h"


#define L1_PORT         P2
#define L1_GPIO_PIN     DEF_GPIO(1)    
#define L1_PIN          DEF_PIN(P2,1)   
#define L1_UP           DEF_UP(P2)
#define L1_TRIS         DEF_TRIS(P2)   

#define L2_PORT         P2
#define L2_GPIO_PIN     DEF_GPIO(2)    
#define L2_PIN          DEF_PIN(P2,2)   
#define L2_UP           DEF_UP(P2)
#define L2_TRIS         DEF_TRIS(P2)  

#define L3_PORT         P2
#define L3_GPIO_PIN     DEF_GPIO(3)    
#define L3_PIN          DEF_PIN(P2,3)   
#define L3_UP           DEF_UP(P2)
#define L3_TRIS         DEF_TRIS(P2)  

#define L4_PORT         P2
#define L4_GPIO_PIN     DEF_GPIO(4)    
#define L4_PIN          DEF_PIN(P2,4)   
#define L4_UP           DEF_UP(P2)
#define L4_TRIS         DEF_TRIS(P2)  

#define L5_PORT         P2
#define L5_GPIO_PIN     DEF_GPIO(5)    
#define L5_PIN          DEF_PIN(P2,5)   
#define L5_UP           DEF_UP(P2)
#define L5_TRIS         DEF_TRIS(P2)  

#define R1_PORT         P1
#define R1_GPIO_PIN     DEF_GPIO(7)    
#define R1_PIN          DEF_PIN(P1,7)   
#define R1_UP           DEF_UP(P1)
#define R1_TRIS         DEF_TRIS(P1)   

#define R2_PORT         P1
#define R2_GPIO_PIN     DEF_GPIO(6)    
#define R2_PIN          DEF_PIN(P1,6)   
#define R2_UP           DEF_UP(P1)
#define R2_TRIS         DEF_TRIS(P1)   

#define R3_PORT         P1
#define R3_GPIO_PIN     DEF_GPIO(5)    
#define R3_PIN          DEF_PIN(P1,5)   
#define R3_UP           DEF_UP(P1)
#define R3_TRIS         DEF_TRIS(P1)   

#define R4_PORT         P1
#define R4_GPIO_PIN     DEF_GPIO(4)    
#define R4_PIN          DEF_PIN(P1,4)   
#define R4_UP           DEF_UP(P1)
#define R4_TRIS         DEF_TRIS(P1)  

#define R5_PORT         P1
#define R5_GPIO_PIN     DEF_GPIO(3)    
#define R5_PIN          DEF_PIN(P1,3)   
#define R5_UP           DEF_UP(P1)
#define R5_TRIS         DEF_TRIS(P1)  

#define SET_IN(PORT_PIN)        do{PORT_PIN##_PIN = 1;  GPIO_ENABLE_INPUT(PORT_PIN##_TRIS,PORT_PIN##_GPIO_PIN);GPIO_ENABLE_UP(PORT_PIN##_UP,PORT_PIN##_GPIO_PIN); }while(0)
#define SET_OUT(PORT_PIN,hl)    do{PORT_PIN##_PIN = hl; GPIO_ENABLE_OUTPUT(PORT_PIN##_TRIS,PORT_PIN##_GPIO_PIN);GPIO_DISABLE_UP(PORT_PIN##_UP,PORT_PIN##_GPIO_PIN); }while(0)
#define GET_PIN(pin)            pin##_PIN    


extern volatile uint32 rKeyReadBuf;
extern xdata uint32 RKeyChkBuf,RKeyCvtBuf,RKeyOldBuf;

enum
{
    KEY1 = 0,
    KEY2 ,
    KEY3 ,
    KEY4 ,
    KEY5 ,
    KEY6 ,
    KEY7 ,
    KEY8 ,
    KEY9 ,
    KEY10 ,
    KEY11 ,
    KEY12 ,
    KEY13 ,
    KEY14 ,
    KEY15 ,
    KEY16 ,
    KEY17 ,
    KEY18 ,
    KEY19 ,
    KEY20 ,
    KEY21 ,
    KEY22 ,
    KEY23 ,
    KEY24 ,
    KEY25 ,
    KEY_COMBO1,         //  ��ϼￄ1�71ￄ1�77 1 - ����ң����
    KEY_COMBO2,         //  ��ϼￄ1�71ￄ1�77 2 - ���ù���ģʽ
};


//---- ������������ ----//
#define CODE_NOKEY      0x00000000ul
#define	CODE_KEY1		0x00000001ul
#define	CODE_KEY2		0x00000002ul
#define	CODE_KEY3		0x00000004ul
#define	CODE_KEY4		0x00000008ul
#define	CODE_KEY5		0x00000010ul
#define	CODE_KEY6		0x00000020ul
#define	CODE_KEY7		0x00000040ul
#define	CODE_KEY8		0x00000080ul
#define	CODE_KEY9		0x00000100ul
#define	CODE_KEY10		0x00000200ul
#define	CODE_KEY11		0x00000400ul
#define	CODE_KEY12		0x00000800ul
#define	CODE_KEY13		0x00001000ul
#define	CODE_KEY14		0x00002000ul
#define	CODE_KEY15		0x00004000ul
#define	CODE_KEY16		0x00008000ul
#define	CODE_KEY17		0x00010000ul
#define	CODE_KEY18		0x00020000ul
#define	CODE_KEY19		0x00040000ul
#define	CODE_KEY20		0x00080000ul
#define	CODE_KEY21		0x00100000ul
#define	CODE_KEY22		0x00200000ul
#define	CODE_KEY23		0x00400000ul
#define	CODE_KEY24		0x00800000ul
#define	CODE_KEY25		0x01000000ul
#define CODE_COMBO_K1  (CODE_KEY6+CODE_KEY7)
#define CODE_COMBO_K2  (CODE_KEY1+CODE_KEY5)


#define	TM_DBC_KEY		    5       //---- 消抖时间
#define C_MAX_KEY_PRESS     2       //---- 最多2键
#define C_MAX_KEY_NUM       25      //---- 按键总数（包括组合键）
#define C_MAX_EXT_KEY_NUM   5       //---- 扩展按键数量（前后按组合键）
#define INVAILD_KEY         0xFF    //---- 无效按键

#define KEY_YEDENG                  KEY19
#define KEY_ZUOWEN                  KEY7
#define KEY_CHONGSHUI               KEY11
#define KEY_PAOMODUN                KEY22
#if _DEBUG_EN_
#define KEY_FANGAI                  KEY13
#define KEY_FANQUAN                 KEY12
#else
#define KEY_FANGAI                  KEY1
#define KEY_FANQUAN                 KEY2
#endif
#define CODE_KEY_YEDENG             CODE_KEY19
#define CODE_KEY_ZUOWEN             CODE_KEY7
#define CODE_KEY_CHONGSHUI          CODE_KEY11
#define CODE_KEY_PAOMODUN           CODE_KEY22
#if _DEBUG_EN_
#define CODE_KEY_FANGAI             CODE_KEY13
#define CODE_KEY_FANQUAN            CODE_KEY12
#else
#define CODE_KEY_FANGAI             CODE_KEY1
#define CODE_KEY_FANQUAN            CODE_KEY2
#endif


#define KEY_COMBO_BASE              KEY_ZUOWEN
#define CODE_KEY_COMBO_BASE         CODE_KEY_ZUOWEN
#define CODE_KEY_COMBO1_OFFSET      CODE_KEY_CHONGSHUI
#define CODE_KEY_COMBO2_OFFSET      CODE_KEY_PAOMODUN
#define CODE_KEY_COMBO3_OFFSET      CODE_KEY_FANGAI
#define CODE_KEY_COMBO4_OFFSET      CODE_KEY_FANQUAN
#define CODE_KEY_COMBO5_OFFSET      CODE_KEY_YEDENG

#define KEY_COMBO1_OFFSET   0
#define KEY_COMBO2_OFFSET   1
#define KEY_COMBO3_OFFSET   2
#define KEY_COMBO4_OFFSET   3
#define KEY_COMBO5_OFFSET   4

extern xdata void(*KeyShortFunc[C_MAX_KEY_NUM+C_MAX_EXT_KEY_NUM])(void);
extern xdata void(*KeyLongFunc[C_MAX_KEY_NUM+C_MAX_EXT_KEY_NUM])(void);

extern uint8 RKeyCode;


void Key_Read(void);
void initKEY(void);
void Key_PowerSaveSet(void);
void Key_Main(void);
void MultiKey_Proc(void);

#endif

